package com.ukao.cashregister.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.Dimension;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.SystemUIUtils;
import com.ukao.cashregister.widget.StateImageView;

import java.text.ParseException;
import java.util.Calendar;

import static com.ukao.cashregister.R.id.close_btn;
import static com.ukao.cashregister.utils.MyDateUtils.getTime;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class DialogTool {


    private volatile static DialogTool singleton;
    private OptionsPickerView pvOptins;
    private TimePickerView pvCustomTime;



    public  DialogTool (){

    }
    public static DialogTool getSingleton() {
        if (singleton == null) {
            synchronized (DialogTool.class) {
                if (singleton == null) {
                    singleton = new DialogTool();
                }
            }
        }
        return singleton;
    }


    /**
     *城市选择器
     */
    public   OptionsPickerView ShowCityPickerView(Context _mActivity, OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener) {

        View view = LayoutInflater.from(_mActivity).inflate(R.layout.layout_basepickerview, null);
            pvOptins= new OptionsPickerView.Builder(_mActivity, mOnOptionsSelectListener)
                .isDialog(true)
                .setDecorView((ViewGroup) view)
                .setDividerColor(R.color.gray)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(CheckUtils.isMIUIDevices()?17:26)
                .setLayoutRes(R.layout.pickerview_custom_city, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.title);

                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptins.dismiss();
                            }
                        });
                        picker_title.setText("选择省份");
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptins.returnData();
                                 pvOptins.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptins.dismiss();
                            }
                        });

                    }
                })
                .setOutSideCancelable(true)// default is true
                .build();

     return  pvOptins;
    };


    public  void showDatePicker(String birthday,String timeType, Context  mContext,TimePickerView.OnTimeSelectListener mOnTimeSelectListener ) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();

        try {


            if (!TextUtils.isEmpty(birthday)){
                selectedDate.setTime(getTime(birthday, timeType));
            }else{
                selectedDate.add(Calendar.YEAR,-25);
                selectedDate.setTime(selectedDate.getTime());
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        //0代表1月
        startDate.set(1900,0,1);
        Calendar endDate = Calendar.getInstance();
//        endDate.set(Calendar.getInstance().get(Calendar.YEAR), 11,31);
        //时间选择器 ，自定义布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_basepickerview, null);
        pvCustomTime= new TimePickerView.Builder(mContext, mOnTimeSelectListener)
                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .isDialog(true)
                .setLabel("年", "月", "日", "", "", "")
                .setTextColorCenter(Color.BLACK)
                .setContentSize( CheckUtils.isMIUIDevices()?17:26)
                .setDecorView((ViewGroup) view)

                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(R.id.close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.title);
                        picker_title.setText("添加生日");
                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }

                })

                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(R.color.gray)
                .build();
        pvCustomTime.show();

    }


    /**
     * 下单时间段
     * @param showDays
     * @param mRightBt3
     * @param mRightBt1
     * @param mContext
     */
    public void showDateQuantumPicker(final int showDays,final Button mRightBt3,final  Button mRightBt1,Context mContext,
                                      TimePickerView.OnTimeSelectListener mOnTimeSelectListener) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();
        try {

            String time = showDays > 7 ? mRightBt3.getText().toString() : mRightBt1.getText().toString();
            time = Calendar.getInstance().get(Calendar.YEAR) + "—" + time;
            selectedDate.setTime(getTime(time, "yyyy—MM—dd EEE"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        startDate.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, showDays);
//        endDate.set(Calendar.getInstance().get(Calendar.YEAR), 12,31);
        //时间选择器 ，自定义布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_basepickerview, null);
        pvCustomTime = new TimePickerView.Builder(mContext, mOnTimeSelectListener
        )

                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .isDialog(true)
                .setLabel("", "月", "日", "", "", "")
                .setTextColorCenter(Color.BLACK)
                .setContentSize( CheckUtils.isMIUIDevices()?17:26)
                .setDecorView((ViewGroup) view)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.title);

                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });

                        picker_title.setText(showDays > 6?"送件日期":"取件日期");
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }

                })
                .setType(new boolean[]{false, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(R.color.gray)
                .build();
        pvCustomTime.show();

    }


    public void showDialog(Context  mContext,String tipaymentPricetle,String sendCarriage,String takeCarriage ,String  modifyPrice,final  DialogInterface.OnCancelListener   mOnCancelListener){


        final Dialog dialog =
                new Dialog(mContext,R.style.MyDialog);
        dialog.setCanceledOnTouchOutside(true);
        View   view=LayoutInflater.from(mContext).inflate(R.layout.dialog_amount_of_detail,null);

        dialog.setContentView(view);
        dialog.setOnCancelListener(mOnCancelListener
        );



        StateImageView  closeBtn = (StateImageView) view.findViewById(R.id.close_btn);
        TextView  title = (TextView) view.findViewById(R.id.title);
        LinearLayout  chagePiceLayout = (LinearLayout) view.findViewById(R.id.chage_pice_layout);
        TextView  chagepice = (TextView) view.findViewById(R.id.chage_pice);
        TextView   dialogTotalClothing= (TextView) view.findViewById(R.id.dialog_total_clothing);
        TextView   dialogPickUpFee= (TextView) view.findViewById(R.id.dialog_pick_up_fee);
        TextView   dialogDeliveryFee= (TextView) view.findViewById(R.id.dialog_delivery_fee);
        title.setText("金额明细");
        dialogTotalClothing.setText("￥"+tipaymentPricetle);
        dialogPickUpFee.setText("￥"+takeCarriage);
        dialogDeliveryFee.setText("￥"+sendCarriage);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnCancelListener.onCancel(dialog);
                dialog.dismiss();

            }
        });
        if (CheckUtils.isEmpty(modifyPrice)) {chagePiceLayout.setVisibility(View.GONE);}else{chagePiceLayout.setVisibility(View.VISIBLE); chagepice.setText("￥"+modifyPrice);}

        if (dialog!=null)
        SystemUIUtils.setStickFullScreen(dialog.getWindow());
        dialog.show();
    }

    /**
     * 分析时间
     * @param birthday
     * @param timeType
     * @param mContext
     * @param mOnTimeSelectListener
     */
    public  void showAnalyzeDatePicker(String birthday,String timeType,boolean isDay, Context  mContext,TimePickerView.OnTimeSelectListener mOnTimeSelectListener ) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();

        try {


            if (!TextUtils.isEmpty(birthday)){
                selectedDate.setTime(getTime(birthday, timeType));
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
       String labelDay="";

        boolean[] type;
        if (isDay){
                labelDay="日";
            type = new boolean[]{true, true, true, false, false, false};
            }else{

                type = new boolean[]{true, true, false, false, false, false};
            }

        Calendar startDate = Calendar.getInstance();
        //0代表1月
        startDate.set(2000,1,1);
        Calendar endDate = Calendar.getInstance();
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_basepickerview, null);
        pvCustomTime= new TimePickerView.Builder(mContext, mOnTimeSelectListener)
                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .isDialog(true)
                .setLabel("年", "月",labelDay, "", "", "")
                .setTextColorCenter(Color.BLACK)
                .setContentSize( CheckUtils.isMIUIDevices()?17:26)
                .setDecorView((ViewGroup) view)

                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.title);
                        picker_title.setText("选择日期");
                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }

                })

                .setType(type)
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(R.color.gray)
                .build();
        pvCustomTime.show();

    }




}
